<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;
class Common extends Controller{
	//保存管理员数据的变量
	public $info = array();

	public function __construct()
    {
    	parent::__construct();
		define('MODULE_NAME',$this->request->module());  // 当前模块名称是
		define('CONTROLLER_NAME',$this->request->controller()); // 当前控制器名称
		define('ACTION_NAME',$this->request->action()); // 当前操作名称是
    	$this->checkLogin();
		$this->check_priv();
    }

    //判断是否已经登陆
	private function checkLogin()
	{
		$this->info = session("info");
//		var_dump(session("info"));
		if(!$this->info){
			$this->redirect("Login/login");
		}else{
			$this->assign("info",$this->info);
		}
	}

	//检测权限
	public function check_priv()
	{

		$ctl = CONTROLLER_NAME;
		$act = ACTION_NAME;
		$act_list = session('act_list');
//		halt($act_list);
		//$act_list="64,65,66";
		//无需验证的操作
		$uneed_check = array('login','logout','vertifyHandle','vertify','imageUp','upload','login_task');
		$this->assign("active",$ctl.'/'.$act);
		$this->assign("power",$ctl.'@'.$act);
		if($act_list == 'all'){
			$this->assign("role_right",'all');
			$this->assign("group_power",'all');
			return true;
		}elseif(strpos($act,'ajax') || in_array($act,$uneed_check)){
			//所有ajax请求不需要验证权限
			return true;
		}else{
			$group = db('system_menu')->where("id", "in", $act_list)->column('group');
			$group_power='';
			foreach ($group as $v){
				$group_power .= $v.',';
			}
			$right = db('system_menu')->where("id", "in", $act_list)->column('right');
			$role_right='';
			foreach ($right as $val){
				$role_right .= $val.',';
			}
			$role_right = explode(',', $role_right);
			$group_power = explode(',', $group_power);
			//检查是否拥有此操作权限
			if(!in_array($ctl.'@'.$act, $role_right) && $ctl!='Index'){
				//$this->error('您没有操作权限,请联系超级管理员分配权限',url('Index/index'));
			}
			$this->assign("role_right",$role_right);
			$this->assign("group_power",$group_power);
		}
	}

	public function auth_group()
	{
		$data = db('auth_group')->select();
		foreach($data as $k=>$v){
			$group[$data[$k]['key']]=$data[$k]['val'];
		}
		return $group;
	}

	//获取下级会员
	public function next_member($uid){

        //二级
        $data['team'] = db('team')->alias('t')->join('user_account a','a.id = t.uid','LEFT')->where('t.recommend_id',$uid)->select();

        foreach ($data['team'] as $key => $value) {

        	if(!empty($value['uid'])){

        		//三级
            	$data['team3'] = db('team')->alias('t')->join('user_account a','a.id = t.uid','LEFT')->where('t.recommend_id',$value['uid'])->select();
        	}else{
        		$data['team3'] =[];
        	}
            
        }

        return $data;

	}

}